Skip to content

StarDelegate

Base classes

Name Children Inherits
StyledItemDelegate
prettyqt.widgets.styleditemdelegate

⋔ Inheritance diagram

graph TD
  1055912865["itemdelegates.StarDelegate"]
  4109708588["widgets.StyledItemDelegate"]
  3153451512["widgets.AbstractItemDelegateMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  3599391368["QtWidgets.QStyledItemDelegate"]
  475576970["QtWidgets.QAbstractItemDelegate"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  4109708588 --> 1055912865
  3153451512 --> 4109708588
  3227531497 --> 3153451512
  2751680898 --> 3227531497
  2751680898 --> 3153451512
  3599391368 --> 4109708588
  475576970 --> 3599391368
  3984060476 --> 475576970
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: StyledItemDelegate

A delegate class that allows us to render our star ratings.

Source code in prettyqt/itemdelegates/stardelegate.py
class StarDelegate(widgets.StyledItemDelegate):
    """A delegate class that allows us to render our star ratings."""

    ID = "star"

    def paint(
        self,
        painter: gui.QPainter,
        option: widgets.QStyleOptionViewItem,
        index: core.ModelIndex,
    ):
        rating = index.data()
        if rating is None:
            super().paint(painter, option, index)
            return
        star_rating = StarRating(rating)

        # If the row is currently selected, we need to make sure we
        # paint the background accordingly.
        if option.state & widgets.Style.StateFlag.State_Selected:
            # The original C++ example used option.palette.foreground() to
            # get the brush for painting, but there are a couple of
            # problems with that:
            #   - foreground() is obsolete now, use windowText() instead
            #   - more importantly, windowText() just returns a brush
            #     containing a flat color, where sometimes the style
            #     would have a nice subtle gradient or something.
            # Here we just use the brush of the painter object that's
            # passed in to us, which keeps the row highlighting nice
            # and consistent.
            painter.fillRect(option.rect, painter.brush())

        # Now that we've painted the background, call star_rating.paint()
        # to paint the stars.
        star_rating.paint(painter, option.rect, option.palette)

    def sizeHint(self, option: widgets.QStyleOptionViewItem, index: core.ModelIndex):
        """Return the size needed to display the item in a QSize object."""
        rating = index.data()
        star_rating = StarRating(rating if rating is not None else 0)
        return star_rating.sizeHint()

    # The next 4 methods handle the custom editing that we need to do.
    # If this were just a display delegate, paint() and sizeHint() would
    # be all we needed.

    def createEditor(
        self,
        parent: widgets.QWidget,
        option: widgets.QStyleOptionViewItem,
        index: core.ModelIndex,
    ):
        """Create and return the StarEditor object we'll use to edit the StarRating."""
        editor = StarEditor(parent)
        editor.editing_finished.connect(self.commitAndCloseEditor)
        return editor

    def setEditorData(self, editor: StarEditor, index: core.ModelIndex):
        """Set the data to be displayed and edited by our custom editor."""
        rating = index.data()
        editor.set_star_rating(rating if rating is not None else 0)

    def setModelData(
        self,
        editor: widgets.QWidget,
        model: core.QAbstractItemModel,
        index: core.ModelIndex,
    ):
        """Get the data from our custom editor and stuffs it into the model."""
        model.setData(index, cast("StarEditor", editor).star_rating.star_count)

    def commitAndCloseEditor(self):
        editor = self.sender()

        # The commitData signal must be emitted when we've finished editing
        # and need to write our changed back to the model.
        self.commitData.emit(editor)
        self.closeEditor.emit(editor, self.EndEditHint.NoHint)

createEditor(parent: widgets.QWidget, option: widgets.QStyleOptionViewItem, index: core.ModelIndex)

Create and return the StarEditor object we'll use to edit the StarRating.

Source code in prettyqt/itemdelegates/stardelegate.py
def createEditor(
    self,
    parent: widgets.QWidget,
    option: widgets.QStyleOptionViewItem,
    index: core.ModelIndex,
):
    """Create and return the StarEditor object we'll use to edit the StarRating."""
    editor = StarEditor(parent)
    editor.editing_finished.connect(self.commitAndCloseEditor)
    return editor

setEditorData(editor: StarEditor, index: core.ModelIndex)

Set the data to be displayed and edited by our custom editor.

Source code in prettyqt/itemdelegates/stardelegate.py
def setEditorData(self, editor: StarEditor, index: core.ModelIndex):
    """Set the data to be displayed and edited by our custom editor."""
    rating = index.data()
    editor.set_star_rating(rating if rating is not None else 0)

setModelData(editor: widgets.QWidget, model: core.QAbstractItemModel, index: core.ModelIndex)

Get the data from our custom editor and stuffs it into the model.

Source code in prettyqt/itemdelegates/stardelegate.py
def setModelData(
    self,
    editor: widgets.QWidget,
    model: core.QAbstractItemModel,
    index: core.ModelIndex,
):
    """Get the data from our custom editor and stuffs it into the model."""
    model.setData(index, cast("StarEditor", editor).star_rating.star_count)

sizeHint(option: widgets.QStyleOptionViewItem, index: core.ModelIndex)

Return the size needed to display the item in a QSize object.

Source code in prettyqt/itemdelegates/stardelegate.py
def sizeHint(self, option: widgets.QStyleOptionViewItem, index: core.ModelIndex):
    """Return the size needed to display the item in a QSize object."""
    rating = index.data()
    star_rating = StarRating(rating if rating is not None else 0)
    return star_rating.sizeHint()

:fa-brands-github: Show source on GitHub